// miniprogram/pages/index/index.js
Page({
  data: {
    userInfo: {},
    sceneType: '',
    sceneTypeName: '',
    totalPoints: 0,
    rank: 0,
    todayPoints: 0,
    lastLogin: '',
    quickActions: [
      { id: 1, name: '添加积分', icon: 'add', url: '/pages/record/add' },
      { id: 2, name: '积分规则', icon: 'rule', url: '/pages/rule/list' },
      { id: 3, name: '我的记录', icon: 'list', url: '/pages/record/list' },
      { id: 4, name: '积分统计', icon: 'stats', url: '/pages/statistics/index' },
      { id: 5, name: '积分兑换', icon: 'redeem', url: '/pages/redeem/index' }
    ],
    pendingAuditCount: 0
  },

  onLoad() {
    this.loadData()
  },

  onShow() {
    this.loadData()
  },

  loadData() {
    const app = getApp()
    const userInfo = app.globalData.userInfo
    const sceneType = app.globalData.sceneType
    
    this.setData({
      userInfo: userInfo,
      sceneType: sceneType,
      sceneTypeName: app.getSceneTypeName(sceneType),
      totalPoints: 1250, // 模拟数据
      rank: 2, // 模拟数据
      todayPoints: 30, // 模拟数据
      pendingAuditCount: 3, // 模拟数据
      lastLogin: this.formatDate(new Date()) // 添加上次登录时间
    })
  },

  goToPage(e) {
    const url = e.currentTarget.dataset.url
    if (url) {
      // 定义tabBar页面路径
      const tabBarPages = [
        '/pages/index/index',
        '/pages/record/list',
        '/pages/statistics/index',
        '/pages/user/profile'
      ]
      
      // 判断是否为tabBar页面
      if (tabBarPages.includes(url)) {
        wx.switchTab({
          url: url
        })
      } else {
        wx.navigateTo({
          url: url
        })
      }
    }
  },

  goToAuditList() {
    wx.navigateTo({
      url: '/pages/audit/list'
    })
  },

  // 格式化日期
  formatDate(date) {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
  }
})